load("@bazel_skylib//rules:expand_template.bzl", "expand_template")
load("@rules_cc//cc:cc_library.bzl", "cc_library")

UPSTREAM_VERSION = module_version().split(".bcr.", 1)[0]

UPSTREAM_VERSION_MAJOR = UPSTREAM_VERSION.split(".")[0]

UPSTREAM_VERSION_MINOR = UPSTREAM_VERSION.split(".")[1]

UPSTREAM_VERSION_PATCH = UPSTREAM_VERSION.split(".")[2]

expand_template(
    name = "config_h",
    out = "src/config.h",
    substitutions = {
        "{LIBXI_VERSION}": UPSTREAM_VERSION,
        "{LIBXI_VERSION_MAJOR}": UPSTREAM_VERSION_MAJOR,
        "{LIBXI_VERSION_MINOR}": UPSTREAM_VERSION_MINOR,
        "{LIBXI_VERSION_PATCH}": UPSTREAM_VERSION_PATCH,
    },
    template = "src/config.h.in",
)

cc_library(
    name = "xi",
    srcs = glob(
        [
            "src/*.c",
            "src/*.h",
        ],
        # This doesn't seem to even compile, and isn't necessary.
        exclude = ["src/XFreeLst.c"],
    ) + [
        "src/config.h",
    ],
    hdrs = glob(["include/X11/extensions/*.h"]),
    copts = [
        "-DHAVE_CONFIG_H",
        "-Wno-implicit-int",
    ],
    includes = [
        "include",
        "include/X11/extensions",
        "src",
    ],
    deps = [
        "@libx11",
        "@libxext",
        "@libxfixes",
    ],
)

alias(
    name = "libxi",
    actual = ":xi",
    visibility = ["//visibility:public"],
)
